<?php
class Cp_ResourceController extends Pidguin_Controller_Cp_Action
{	
	public function indexAction()
	{
		$this->view->resources = Model_Resource::getInstance()->fetchAll();
	}
	
	public function editAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Resource ID is not set');
		}
		
		//@todo: разрешить редактировать ресурсы 1 и 2 только для root
		 
		$m = Model_Resource::getInstance();
		$select = $m->select()->where(Model_Resource::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		 
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Resource not found');
		}
		
		$_module = $row->findParentRow('Model_Module');
		
		$this->view->resource = $row;
		$this->view->modules = Model_Module::getInstance()->fetchAll();
		$this->view->module = $_module;
	}
	
	public function editSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('User ID is not set');
		}
	
		if (!$this->_getParam('key')) {
			throw new Zend_Controller_Action_Exception('Key is not set');
		}
	
		if (!preg_match('/[a-z0-9_]*/i', $this->_getParam('key'))) {
			throw new Zend_Controller_Action_Exception('key incorrect');
		}
	
		if (!$this->_getParam('description')) {
			throw new Zend_Controller_Action_Exception('Description is not set');
		}
	
		if (!preg_match('/[a-zа-я0-9_\s\-\.]*/i', $this->_getParam('description'))) {
			throw new Zend_Controller_Action_Exception('Description incorrect');
		}
	
		if (!$this->_getParam('resource_action')) {
			throw new Zend_Controller_Action_Exception('Action is not set');
		}
	
		if (!preg_match('/[a-z\.\*]+\:\:[a-z\.\*]+/i', $this->_getParam('resource_action'))) {
			throw new Zend_Controller_Action_Exception('Action incorrect');
		}
		
		if (!$this->_getParam('module')) {
			throw new Zend_Controller_Action_Exception('Module is not set');
		}
		
		$module_select = Model_Module::getInstance()->select()->where(
			Model_Module::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('module_id'))
		);
		
		$module = Model_Module::getInstance()->fetchRow($module_select);
		
		if (!$module) {
			throw new Zend_Controller_Action_Exception('Module not found');
		}
		
		$model = Model_Resource::getInstance();
		$select = $model->select()->where(Model_Resource::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $model->fetchRow($select);
		
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Resource not found');
		}
		
		$row->name = $this->_getParam('key');
		$row->description = $this->_getParam('description');
		$row->module_id = $module->id;
		$row->action = $this->_getParam('resource_action');
		$row->active = 1;
		$row->modified = date('Y-m-d H:i:s');
		$row->save();
		$this->_helper->json(array('status' => 'ok'));
	}
	
	public function deleteAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Resource id is not set');
		}
		
		if ($this->_getParam('id') == '1' || 
				$this->_getParam('id') == '2') {
			throw new Zend_Controller_Action_Exception('You can not delete system resource');
		}
	
		$m = Model_Resource::getInstance();
		$select = $m->select()->where(Model_Resource::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Resource not found');
		}
		$row->delete();
		$this->_redirect('/cp/resource');
	}
	
	public function createAction()
	{
		$this->view->modules = Model_Module::getInstance()->fetchAll();
	}
	
	public function createSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
	
		if (!$this->_getParam('key')) {
			throw new Zend_Controller_Action_Exception('Key is not set');
		}
	
		if (!preg_match('/[a-z0-9_]*/i', $this->_getParam('key'))) {
			throw new Zend_Controller_Action_Exception('key incorrect');
		}
	
		if (!$this->_getParam('description')) {
			throw new Zend_Controller_Action_Exception('Description is not set');
		}
	
		if (!preg_match('/[a-zа-я0-9_\s\-\.]*/i', $this->_getParam('description'))) {
			throw new Zend_Controller_Action_Exception('Description incorrect');
		}
	
		if (!$this->_getParam('resource_action')) {
			throw new Zend_Controller_Action_Exception('Action is not set');
		}
	
		if (!preg_match('/[a-z\.\*]+\:\:[a-z\.\*]+/i', $this->_getParam('resource_action'))) {
			throw new Zend_Controller_Action_Exception('Action incorrect');
		}
		
		if (!$this->_getParam('module')) {
			throw new Zend_Controller_Action_Exception('Module is not set');
		}
		
		$module_select = Model_Module::getInstance()->select()->where(
			Model_Module::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('module_id'))
		);
		
		$module = Model_Module::getInstance()->fetchRow($module_select);
		
		if (!$module) {
			throw new Zend_Controller_Action_Exception('Module not found');
		}
		
		$model = Model_Resource::getInstance();
		$select = $model->select()->where(Model_Resource::getDefaultAdapter()->quoteInto('name = ?', $this->_getParam('key')));
		$row = $model->fetchRow($select);
		
		if ($row) {
			throw new Zend_Controller_Action_Exception('Resource ' . $this->_getParam('key') . ' is already created');
		}
		
		$row = Model_Resource::getInstance()->createRow();		
		$row->name = $this->_getParam('key');
		$row->description = $this->_getParam('description');
		$row->module_id = $module->id;
		$row->action = $this->_getParam('resource_action');
		$row->active = 1;
		$row->created = date('Y-m-d H:i:s');
		$row->save();
		$this->_helper->json(array('status' => 'ok'));
	}
}